// 注册一些全局变量
var isOnline = true;

angular.module('wenews', ['ionic', 'wenews.controllers', 'wenews.service', 'ngCordova'])

    .run(function ($rootScope, $ionicPlatform, $location, $timeout, $ionicHistory, $cordovaToast, $cordovaAppVersion, $cordovaNetwork, $http, $ionicPopup) {
        $ionicPlatform.ready(function () {
            // 隐藏 SplashScreen
            if (navigator && navigator.splashscreen) {
                setTimeout(function () {
                    navigator.splashscreen.hide();
                }, 100);
            }

            // Hide the accessory bar by default (remove this to show the accessory bar above the keyboard
            // for form inputs)
            if (window.cordova && window.cordova.plugins.Keyboard) {
                cordova.plugins.Keyboard.hideKeyboardAccessoryBar(true);
                cordova.plugins.Keyboard.disableScroll(true);

            }
            if (window.StatusBar) {
                // org.apache.cordova.statusbar required
                StatusBar.styleDefault();
            }

            // 网络判断
            isOnline = $cordovaNetwork.isOnline();
            if (!isOnline) {
                $cordovaToast.showShortBottom('没有网络连接');
                $location.path("/offline");
            }
            $rootScope.$on('$cordovaNetwork:online', function (event, networkState) {
                isOnline = true;
            });
            $rootScope.$on('$cordovaNetwork:offline', function (event, networkState) {
                isOnline = false;
            });


            // 更新
            $cordovaAppVersion.getVersionNumber().then(function (version) {
                var url = "http://www.cnionic.com/web/index.php?s=/app/version/update/name/wenews";
                $http.get(url).success(function (ret) {
                    if (ret.status == 1) {
                        if (version != ret.data.version) {
                            var confirmPopup = $ionicPopup.confirm({
                                title: '新版本出炉，是否要升级？',
                                template: ret.data.description, // 更新内容
                                cancelText: '取消',
                                okText: '确定'
                            });
                            var downloadUrl = ret.data.url;
                            confirmPopup.then(function (res) {
                                if (res) {
                                    // todo 保存更新日期到本地 , 检查更新，每天一次
                                    // 调用系统浏览器下载
                                    window.open(downloadUrl, '_system', 'location=yes');
                                }
                            });
                        }
                    }
                });
            });
        });

        //双击退出
        $ionicPlatform.registerBackButtonAction(function (e) {
            if ($rootScope.backButtonPressedOnceToExit) {
                ionic.Platform.exitApp();
            }
            else if ($ionicHistory.backView()) {
                $ionicHistory.goBack();
            } else {
                $rootScope.backButtonPressedOnceToExit = true;
                $cordovaToast.showShortBottom('再按一次退出');
                setTimeout(function () {
                    $rootScope.backButtonPressedOnceToExit = false;
                }, 2000);
            }
            e.preventDefault();
            return false;
        }, 101);
    })

    .config(function ($stateProvider, $urlRouterProvider, $ionicConfigProvider) {
        // 全局配置
        $ionicConfigProvider.backButton.text('  ');
        $ionicConfigProvider.tabs.position("bottom");
        $ionicConfigProvider.tabs.style("standard");
        $ionicConfigProvider.navBar.alignTitle('center');
        if (!ionic.Platform.isIOS())
            $ionicConfigProvider.scrolling.jsScrolling(false);

        $stateProvider.state('app', {
            url: '/app',
            abstract: true,
            templateUrl: 'templates/menu.html',
            controller: 'AppCtrl'
        }).state('app.setting', {
            url: '/setting',
            views: {
                'menuContent': {
                    templateUrl: 'templates/setting.html',
                    controller: 'SettingCtrl'
                }
            }
        }).state('app.list', {
            url: '/list/:id/:title',
            views: {
                'menuContent': {
                    templateUrl: 'templates/list.html',
                    controller: 'ListCtrl'
                }
            }
        }).state('offline', {
            url: '/offline',
            templateUrl: 'templates/offline.html',
            controller: 'OfflineCtrl'
        });
        // if none of the above states are matched, use this as the fallback
        $urlRouterProvider.otherwise('/app/list/pc_9/科技咖');
    });
